<?php

trait Trait_Api_Promo
{
    public function pendant_data()
    {
        $pendant_status = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_promo_pendant_status')->execute()->get('value');
        $pendant_img = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_promo_pendant_img')->execute()->get('value');
        $pendant_link = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_promo_pendant_link')->execute()->get('value');
        $pendant_link = json_decode(urldecode($pendant_link),true);
        unset($pendant_link['title']);
        unset($pendant_link['typename']);
        unset($pendant_link['litpic']);
        unset($pendant_link['dis']);
        unset($pendant_link['navid']);
        unset($pendant_link['subname']);
        $data = [
            'pendant_status' => $pendant_status?1:0,
            'pendant_img'    => $pendant_img?$this->img_url($pendant_img, 200, 200):'',
            'pendant_link'   => $pendant_link,
        ];

        return $data;
    }

    public function coupon_list($coupon_id)
    {
        $coupon = DB::select()->from('coupon')->where('id','in',explode(',',$coupon_id))->execute()->as_array();
        $list = [];
        foreach ($coupon as $v) {
            if ($v['starttime']) {
                $start_date           = date('Y-m-d', $v['starttime']);
                $end_date             = date('Y-m-d', $v['endtime']);
                $v['validity_period'] = $start_date . ' 至 ' . $end_date;
            } else {
                $v['validity_period'] = '永久有效';
            }
            $list[] = [
                'id'              => $v['id'],
                'name'            => $v['name'],
                'validity_period' => $v['validity_period'],
                'type'            => $v['type'],
                'amount'          => $v['amount'],
                'samount'         => $v['samount'],
            ];
        }
        return $list;
    }

    public function img_handle($img_url, $info)
    {
        $piclist = [];
        foreach ($img_url as $key => $img) {
            $data                     = $info['jump_url'][$key]['data'];
            $piclist[$key]['img_url'] = $this->img_url($img, 600, 956);
            switch ($data['ad_type']) {
                case 1:
                    $pinyin = DB::select()->from('model')->where('id', '=', $data['typeid'])->execute()->get('correct');

                    $piclist[$key]['jump_url'] = [
                        'path'    => '/subPackages/' . $pinyin . '/detail?id=' . $data['id'],
                        'type_id' => $data['typeid'],
                        'ad_type' => 1
                    ];
                    break;
                case 2:
                    $piclist[$key]['jump_url'] = [
                        'path'    => $data['path'],
                        'type_id' => $data['typeid'],
                        'ad_type' => 2
                    ];
                    break;
                case 3:
                    $piclist[$key]['jump_url'] = [
                        'path'      => $data['path'],
                        'is_nested' => $data['is_nested'],
                        'ad_type'   => 3
                    ];
                    break;
                case 4:
                    $piclist[$key]['jump_url'] = [
                        'path'    => $data['path'],
                        'app_id'  => $data['appId'],
                        'ad_type' => 4
                    ];
                    break;
            }
        }
        return $piclist;
    }

    public function frequency_record($params)
    {
        $record = DB::select()->from('promo_click')
            ->where('member_id', '=', $params['mid'])
            ->where('promo_id', '=', $params['promo_id'])
            ->execute()->current();
        if($record){
            $number = bcadd($record['number'],1);
            DB::update('promo_click')->set(['number'=>$number,'last_time'=>time()])->where('id','=',$record['id'])->execute();
        }else{
            $data = [
                'promo_id' => $params['promo_id'],
                'member_id' => $params['mid'],
                'number' => 1,
                'last_time' => time()
            ];
            DB::insert('promo_click', array_keys($data))->values(array_values($data))->execute();
        }
    }

    public function coupon_receive($params)
    {
        $promo = DB::select()->from('promo')->where('id','=',$params['promo_id'])->execute()->current();
        if($promo['type']!=3){
            throw new Exception('非优惠券活动');
        }
        $coupon_id = explode(',',$promo['coupon_id']);
        $coupon = DB::select()->from('coupon')
            ->where('id','in',$coupon_id)
            ->and_where('totalnumber','>',0)
            ->execute()->as_array();
        if(!$coupon){
            throw new Exception('优惠券已被领完');
        }
        foreach ($coupon as $v){
            $is_exist = DB::select()->from('member_coupon')
                ->where('mid','=',$params['mid'])
                ->and_where('cid','=',$v['id'])
                ->and_where('is_give','=',1)
                ->execute()->valid();
            if(!$is_exist){
                DB::update('coupon')->set(['totalnumber'=>bcsub($v['totalnumber'],1)])->where('id','=',$v['id'])->execute();
                $data = [
                    'mid'      => $params['mid'],
                    'cid'      => $v['id'],
                    'totalnum' => 1,
                    'usenum'   => 0,
                    'addtime'  => time(),
                    'is_give'  => 1,
                    'givetime' => time()
                ];
                DB::insert('member_coupon', array_keys($data))->values(array_values($data))->execute();
            }
        }
    }
}